<template>
  <div id="partnerShare">
    <el-dialog
      class="sharePageDialog"
      :title="shareTitle"
      :show-close="false"
      :visible.sync="shareVisibleFun"
      @close="destroyshare"
      width="25%"
    >
      <el-tabs v-model="activeName" v-loading='loading' element-loading-text="上链分享中"
    element-loading-spinner="el-icon-loading">
        <el-tab-pane label="合作伙伴分享" name="first">
            <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
            <el-checkbox-group v-model="checkedCompany" @change="handleCheckedCompanyChange">
                <el-checkbox v-for="company in partner" :label="company" :key="company.id">{{company.label}}</el-checkbox>
            </el-checkbox-group>
        </el-tab-pane>
      </el-tabs>
      <span slot="footer" class="dialog-footer">
        <el-button @click="destroyshare">取 消</el-button>
        <el-button type="primary" @click="submitShareFunc()" :disabled="!(checkedCompany.length>0)">分 享</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["sharePara", "shareTitle",'companyList'],
  data() {
    return {
      checkAll:false,
      shareVisibleFun:false,
      activeName: "first",
      isRouterAlive: true,
      isIndeterminate:true,
      checkedCompany:[],
      partner:[],
      loading:false,
    };
  },
  watch: {
    companyList:function(newVal,oldVal){
      if(newVal!=false){

        newVal.forEach(val=>{
          this.partner.push({
            label:val.name,
            id:val.identity,
            floor:1
          })
        })
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    handleCheckAllChange(value){
      console.log(value);
      console.log(this.partner);
      this.checkedCompany = value ? this.partner : [];
      this.isIndeterminate = false;
    },
    handleCheckedCompanyChange(value){
      console.log(value);
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.partner.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.partner.length;
    },
    destroyshare() {
      this.checkedCompany = [];
      this.isRouterAlive = false;
      this.$nextTick(function() {
        this.isRouterAlive = true;
      });
      this.shareVisibleFun = false;
    },
    submitShareFunc(){
      this.loading=true;
      let companyIdList = [];
      if(this.checkedCompany.length>0){
        this.checkedCompany.forEach((val,key)=>{
          companyIdList.push(val.id);
        })
        console.log(companyIdList,this.sharePara[0].id);
        this.$http.post("/chainShare/sharePartner",{
          fileId:this.sharePara[0].id,
          companyId:companyIdList[0]
        }).then(res=>{
          console.log(res);
          if(res.status == 200){
            console.log(res);
            this.loading=false;
            this.shareVisibleFun = false;
            this.$message({
              type:"success",
              message:res.msg
            })
          }
        })
      }else{
        this.$message.error("请先选择要分享的伙伴")
      }
    },
  }
};
</script>

<style scoped lang="scss">
#partnerShare {
  position: relative;
  .el-checkbox-group{
      overflow-y: auto;
      height: 235px;
  }
  .el-checkbox{
      line-height: 30px;
      display: block;
      margin-left: 20px;
  }
}
</style>

